package org.management.system.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.management.base.BaseServiceImpl;
import org.management.config.shiro.manager.TokenManager;
import org.management.system.entity.FileEntity;
import org.management.system.service.FileService;
import org.management.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 功能描述
 * 附件服务类
 * @author jiangyan
 * @date 2019/03/14
 * @description
 * @project management
 */
@Service
public class FileServiceImpl extends BaseServiceImpl<FileEntity> implements FileService {
	/**
	 * 存放路径（项目同级）
	 */
	private static final String UPLOAD = "upload";
	
	/**
	 * 获取存放路径 并创建路径
	 * @param path 存放路径
	 * @return String
	 */
	private String getUploadPath(String path) {
		String realPath = System.getProperty("user.dir");
		File parent = new File(realPath);
		if (StringUtils.isBlank(path)) {
			path = parent.getParent() + File.separator + UPLOAD;
		} else {
			if(!path.startsWith(File.separator)) {
				path = File.separator + path;
			}
			path = parent.getParent() + File.separator + UPLOAD + path;
		}
		File uploadFile = new File(path);
		if (!uploadFile.exists()) {
			boolean mkdirs = uploadFile.mkdirs();
			if (mkdirs) {
				System.out.println("文件创建成功！");
			}
		}
		return uploadFile.getAbsolutePath();
	}
	
	/**
	 * 获取文件下标
	 * @param fileName 文件名称
	 * @return String
	 */
	private String getFileSuffix(String fileName) {
		int index = fileName.lastIndexOf(".");
		String suffix;
		if(index == -1) {
			suffix = "";
		} else {
			suffix = fileName.substring(index + 1);
		}
		return suffix;
	}
	
	
	
	/**
	 * path 存放路径
	 * byte 每次读取字节
	 * parent 父级信息
	 * 
	 */
	@Override
	public void saveFile(MultipartFile file, String parentId) throws IOException {
		String path = null;
		byte [] bytes;
		FileEntity parent = null;
		if(parentId != null) {
			parent = this.selectByKey(parentId);
			if (parent != null) {
				path = getUploadPath(parent.getRelativePath());
			}
		}
		if(path == null) {
			path = getUploadPath(TokenManager.getLoginName());
		}
		String fileName = file.getOriginalFilename();
		String suffix = getFileSuffix(fileName);
		Integer size = (int) file.getSize();
		File wFile = new File(path, fileName);
		if (wFile.exists()) {
			boolean delete = wFile.delete();
			if (delete) {
				System.out.println("文件删除成功");
			}
		} else {
			FileEntity entity = new FileEntity();
			setEntityInfo(entity);
			entity.setFileSize(size.toString());
			entity.setFileName(fileName);
			entity.setSuffix(suffix);
			if(parent != null) {
				entity.setParentId(parentId);
				entity.setRelativePath(parent.getRelativePath());
			}
			this.save(entity);
		}
		boolean newFile = wFile.createNewFile();
		if (newFile) {
			System.out.println("创建新文件成功");
		}
		InputStream is = file.getInputStream();
		if( size < 1024) {
			bytes = new byte[size];
		}else {
			bytes = new byte[1024];
		}
		while (is.read(bytes) != -1) {
			FileUtils.writeByteArrayToFile(wFile, bytes, true);
		}
	}
}
